<?php
class PersonnelExportController extends Zone_Action{
	public function indexAction(){
		$this->removeLayout();
		$fields = array(
			ID			=> array('ID',true),
			code		=> array('Số hiệu',true),
			name		=> array('Tên',true),
			birthday	=> array('Ngày sinh'),		
			functions	=> array('Chức vụ'),
			phone		=> array('Điện thoại'),
			gender		=> array('Giới tính'),
			email		=> array('Email'),
			department_name	=> array('Phòng ban')
		);
			
		if( !isPost() ){	
			$this->set(array(
				fields	=> $fields
			));		
		
		}else{
			$ids = get('ID',array(),true);
		
			if( count($ids) == 0){
				die("<script>
					window.parent.$.Alert('Chưa có bản ghi nào được chọn');
				</script>");
			}			

			$ids = join(",",$ids);	
			
			$posts = $this->Model->fetchAll("
				SELECT 
					`a`.*,
					DATE_FORMAT(`a`.`birthday`,'%d/%m/%Y') as `birthday`,	
					IF(`a`.`gender`='1','Nữ','Nam') as `gender`,	
					`b`.`title` as `department_name`
				FROM `personnels` as `a`
					LEFT JOIN `departments` as `b` ON `b`.`ID`=`a`.`department_id`
				WHERE `a`.`ID` IN ($ids) AND `a`.`is_draft`='no'");	

			$type = get('filetype','xml');		
			loadClass('ZExport');
			
			$export = new ZExport();
			$columns = get('fields',array(),true);

			$export->set($fields, $columns, $posts, $type, get('order') );
			$export->setTitle('Danh sách nhân sự');
			$export->setFile('Office_Export_Personnel');
			$export->export();			
		}	
}}	